from injector import inject
from dataclasses import  dataclass

from internal.entitry.account_entitry import Account
from flask_login import  login_required, current_user

from internal.entitry.tickets_entitry import ProxyType
from internal.schema.tickets_schema import TourGuideWithListSchema, UpdateEnabledSchema, DelTourGuideWithOther, \
    DelAgency, DelUser, UpdatePlayDateSchema, ReSubTickets
from internal.service.tickets_service import TicketsService
from internal.model import  TourGuide,TravelAgency
from pkg.resp import  success_message,success_json
from pkg.paginator.paginator import PageModel
import requests
from internal.exception.exception import FailException
from internal.schema import CreateTicketsForm, TicketsWithListPageSchema, SubTickets, AgencyCheckGuideSchema
from pkg.resp import validate_error_json
from flask import  request

@inject
@dataclass
class TicketsHandler:
    tickets_service:TicketsService
    #保存导游
    def create_tickets(self):
       req = CreateTicketsForm()
       if not req.validate():
           return validate_error_json(req.errors)
       data = self.tickets_service.create_tickets_new(req)
       return success_json(data)

    #保存旅行社
    def created_lvs(self):
        req = AgencyCheckGuideSchema()
        if not req.validate():
            return validate_error_json(req.errors)
        self.tickets_service.lvs(req)
        return success_message("保存成功")
    #查看预约列表
    def query_rservation_tickets_handler(self):
        req = TicketsWithListPageSchema(request.args)
        if not req.validate():
            return validate_error_json(req.errors)
        lis,paginator = self.tickets_service.query_reservation_tickets(req)
        return success_json(PageModel(list=lis, paginator=paginator))
    #导游列表
    def get_tour_guide_with_list(self):
        req = TourGuideWithListSchema(request.args)
        if not req.validate():
            return validate_error_json(req.errors)
        data,paginator = self.tickets_service.get_tour_guide_with_list(req)
        return  success_json(PageModel(list=data, paginator=paginator))
    #删除导游以及旅行社相关
    def del_tickets(self):
        req =  DelTourGuideWithOther()
        if not req.validate():
            return validate_error_json(req.errors)
        self.tickets_service.del_tickets(req.bash_ids.data)
        return success_message()
    #删除旅行社以及成员
    def del_agency(self):
        req = DelAgency()
        if not req.validate():
            return validate_error_json(req.errors)
        self.tickets_service.del_agency(req.agency_ids.data)
        return success_message()
    #删除成员
    def del_members(self):
        req = DelUser()
        if not req.validate():
            return validate_error_json(req.errors)
        self.tickets_service.del_users(req.user_ids.data)
        return success_message()
    #批量更新启用禁用状态
    def update_enabled(self):
        req = UpdateEnabledSchema()
        if not req.validate():
            return validate_error_json(req.errors)
        self.tickets_service.update_enabled(req.enabled.data,req.guide_ids.data)
        return success_message()
    #预约
    def sub(self):
        req = SubTickets()
        if not req.validate():
            return validate_error_json(req.errors)
        if  req.type.data == ProxyType.PROXY.value and not req.secret_id.data:
            raise FailException("secret_id不能为空")
        if req.type.data == ProxyType.PROXY.value and not req.secret_key.data:
            raise FailException("secret_key不能为空")
        self.tickets_service.sub(req.ids.data,req.grounp_ids.data,req.type.data,req.secret_id.data,req.secret_key.data)
        return success_message("执行成功,请等待预约结果，可在预约列表中查看")
    #停止旅行社的预约
    def stop_agency_reservation(self,agency_id:str):
        self.tickets_service.stop_agency_reservation(agency_id)
        return success_message()
    #停止导游预约
    def stop_guide_reservation(self,bash_id:str):
        self.tickets_service.stop_guide_reservation(bash_id)
        return success_message()
    #取消预约
    def cancel(self,order_no:str,bash_id:str,grounp_id:str):

        self.tickets_service.cancel_agency(order_no,bash_id,grounp_id)
        return success_message()
    #失败停止（重新预约）
    def re_sub(self,agency_id:str):

        self.tickets_service.re_sub(agency_id)
        return success_message("执行成功,请等待预约结果")
    #修改预约时间
    def update_play_date(self):
        req = UpdatePlayDateSchema()
        if not req.validate():
            return validate_error_json(req.errors)
        self.tickets_service.update_paly_date(date_str=req.date_str.data,agency_id=req.agency_id.data)
        return success_message()
